<?php
/**
 * Created by PhpStorm.
 * User: jwb
 * Date: 2020/9/17
 * Time: 9:25
 */

namespace App\Services;

use App\Extensions\Exporters\PatentTransactionExporter;
use App\Models\Department;
use App\PatentApply;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Tools\BatchActions;
use Illuminate\Database\Eloquent\Builder;


class PatentTransactionService
{
    public function gridColumn(Grid $grid, $role = 0)
    {
        $user = Admin::user();
        $grid->model()->orderByDesc('id');
        $grid->column('patent_sn', __('申请号'));
        $grid->column('patent_name', __('专利名称'));
        $grid->column('patent_type_id', __('专利类型'))->using(patent_type_select_option())->width(80);
        $grid->column('buyer', __('受让方'));
        $grid->column('department.name', __('负责单位'));
        $grid->column('user.name', __('负责人'));
        $grid->column('contact_person', __('联系人'));
        $grid->column('contact_mobile', __('联系电话'));
        $grid->column('amount', __('转化金额(万)'))->totalRow(function($val){
            return '总计：'.$val.'万';
        })->width(150);
        $grid->column('type', __('转化类型'))->width(80);
        $grid->column('date', __('转化日期'))->width(100);
        $grid->disableCreateButton();
        if ($user->role_type != 1) {
            $grid->batchActions(function (BatchActions $actions) {
                $actions->disableDeleteAndHodeSelectAll();
            });
            $grid->disableActions();
        }
        //$grid->disableFilter();
        $grid->filter(function(Grid\Filter $filter){
            $filter->disableIdFilter();
            $filter->column(1/2,function(Grid\Filter $filter){
                $filter->where(function(Builder $query){
                    $value = $this->input;
                    $query->where('patent_name', 'like', "%$value%")
                        ->orWhere('patent_sn', 'like', "%$value%")
                        ->orWhere('buyer', 'like', "%$value%")
                        ->orWhere('contact_mobile', 'like', "%$value%")
                        ->orWhere('contact_person', 'like', "%$value%")
                        ->orWhereHas('department', function ($query) use ($value) {
                            $query->where('name', 'like', "%$value%");
                        })->orWhereHas('user', function ($query) use ($value) {
                            $query->where('name', 'like', "%$value%");
                        });
                },'关键字','keywords');
                $filter->between('date','转化时间')->date();
            });
            $filter->column(1/2,function(Grid\Filter $filter){
                $filter->equal('department_id','负责单位')->select(Department::departmentOptions());
                $filter->equal('type','转化类型')->checkbox(collect(PatentApply::PTM)->map(function ($vo) {
                    return ['name' => $vo];
                })->pluck('name', 'name'));
                $filter->equal('patent_type_id','专利类型')->checkbox(patent_type_select_option());
            });
        });
        $grid->quickSearch(function ($query, $value) {
            $query->where(function (Builder $query) use ($value) {
                $query->where('patent_name', 'like', "%$value%")
                    ->orWhere('patent_sn', 'like', "%$value%")
                    ->orWhere('buyer', 'like', "%$value%")
                    ->orWhere('contact_mobile', 'like', "%$value%")
                    ->orWhere('contact_person', 'like', "%$value%")
                    ->orWhereHas('department', function ($query) use ($value) {
                        $query->where('name', 'like', "%$value%");
                    })->orWhereHas('user', function ($query) use ($value) {
                        $query->where('name', 'like', "%$value%");
                    });
            });
        })->placeholder('关键字');
        $grid->exporter((new PatentTransactionExporter()));
        return $grid;
    }

}
